import { MapManager } from './MapManager';
import { _decorator, Component, Node } from 'cc';
import Constants from '../data/Constans';
import { PlayerManager } from '../game/PlayerManager';
const { ccclass, property } = _decorator;

@ccclass('GameManager')
export class GameManager extends Component {
    private _GameData = null;

    @property(Node)
    UILobby: Node = null;

    @property(Node)
    UIGame: Node = null;

    @property(Node)
    UIPopup: Node = null;

    @property(Node)
    UIHead: Node = null;

    @property(Node)
    PlayerLayout: Node = null;

    @property(Node)
    MapLayout: Node = null;

    @property(Node)
    Carame: Node = null;

    start() {
        Constants.OnFire.on(Constants.EventName.GO_HOME, this._goHome.bind(this));
        Constants.OnFire.on(Constants.EventName.GO_GAME, this._goGame.bind(this));
        Constants.OnFire.on(Constants.EventName.HOMEBUTTON_STATE, this._homeButtonState.bind(this));
        Constants.OnFire.on(Constants.EventName.GAME_CLEAR, this._gameClear.bind(this));

        Constants.OnFire.on(Constants.EventName.LOADING_MAP, this._loadingMap.bind(this));
        Constants.OnFire.on(Constants.EventName.LOADING_PLAYER, this._loadingPlayer.bind(this));
        Constants.OnFire.on(Constants.EventName.LOADING_PROP, this._loadingProp.bind(this));

        Constants.OnFire.on(Constants.EventName.GAME_STOP, this._stopGame.bind(this));
        Constants.OnFire.on(Constants.EventName.PLAYER_REVIVE, this._revivePlayer.bind(this));

        // Constants.OnFire.on(Constants.EventName.ATTRIBUTE, this._changeAttribute.bind(this));

        // Constants.OnFire.emit(Constants.EventName.ATTRIBUTE, { id: Constants.GameControl.getTrySkill(), type: 1 });

        // Constants.GameControl.setTrySkill(0);
        // Constants.GameControl.setPlayerTryModel(0);

        // Constants.SdkFactory.SdkAPI.init();
        // this.scheduleOnce(() => {
        //     Constants.SdkFactory.SdkAPI.initVideoAd();
        // }, 5);

        // 测试弹窗
        // this.scheduleOnce(() => {
        //     Constants.OnFire.emit(Constants.EventName.ADD_VIEW, { key: 'TrySkillView', value: { path: 'true', } });
        // }, 2);

    }

    // 返回大厅
    private _goHome() {
        this.UIGame.active = false;
        this.UILobby.active = true;
        this.PlayerLayout.active = false;
        this.MapLayout.active = false;
        // Constants.SdkFactory.SdkAPI.hideBarAd();
        // Constants.API.userUpdata().then((res) => { if (res) { printLog('res', res); } });
    }

    // 开始游戏
    private _goGame(event) {
        // if (event.key) {
        //     this.UILobby.active = false;
        //     this.UIGame.active = true;
        // }
        this.UILobby.active = false;
        this.UIGame.active = true;
    }

    // 加载地图
    private _loadingMap() {
        this.MapLayout.active = true;
        this.PlayerLayout.getComponent(MapManager).init();
    }

    // 加载主角和AI
    private _loadingPlayer() {
        this.PlayerLayout.active = true;
        this.PlayerLayout.getComponent(PlayerManager).init();
        // this.PlayerLayout.getComponent(EnemyManager).init();
    }

    // 加载AI人物
    // private _loadingAI() {
    //     this.EnemyLayout.active = true;
    // }

    // 加载道具
    private _loadingProp() {
        // this.PropManager.active = true;
    }

    // 屏蔽返回大厅按钮
    private _homeButtonState(event) {
        // this.UIHome.active = event.key;
    }

    // 清空游戏中的人物等数据
    private _gameClear(event) {
        this.UIGame.active = false;
        this.PlayerLayout.active = false;
        this.MapLayout.active = false;
        // this.PropManager.active = false;
    }

    // 暂停游戏
    private _stopGame(event) {
        Constants.GameControl.setGameState(event.key);
    }

    // 玩家复活
    private _revivePlayer(event) {
        // if (event.key) {
        //     this.PlayerLayout.getComponent(PlayerManager).init();
        // } else {
        //     this.PlayerLayout.getComponent(PlayerManager).closed();
        // }
    }

    // 更换主角的属性
    private _changeAttribute(event) {
        if (event.id == 0) return;
        const a = Constants.GameControl.getGameConfig('player')[event.id];
        const _size = a.size == -1 ? 0 : a.size;
        const _gold = a.gold == -1 ? 0 : a.gold;
        const _quick = a.quick == -1 ? 0 : a.quick;
        const _speed = a.speed == -1 ? 0 : a.speed;

        if (event.type === 1) {
            Constants.GameControl.setPlayerSize(-(_size));
            Constants.GameControl.setPlayerGold(-(_gold));
            Constants.GameControl.setPlayerQuick(-(_quick));
            Constants.GameControl.setPlayerSpeed(-(_speed));

            if (Constants.GameControl.getVideoTrySkill() === 0) {
                Constants.GameControl.setTrySkill(0);
                Constants.GameControl.setPlayerTryModel(0);
            }
        } else {
            Constants.GameControl.setPlayerSize((_size));
            Constants.GameControl.setPlayerGold((_gold));
            Constants.GameControl.setPlayerQuick((_quick));
            Constants.GameControl.setPlayerSpeed((_speed));
        }
    }
}
